package e.g.com.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;

import e.g.com.dao.BaseDao;
import e.g.com.dao.BaseDaoImpl;
import e.g.com.pojo.Tb_topic;
@Service("TopicService")
public class TopicServiceimpl implements TopicService{
	BaseDao<Tb_topic>bd=new BaseDaoImpl<Tb_topic>();
	@SuppressWarnings("unchecked")
	
	public List<Tb_topic> findpage(int first) {
		Query q=bd.findPage("from Tb_topic", null);
		q.setFirstResult(first*10);
		q.setMaxResults(first*10+10);
		return q.list();
	}

	public void insert(Tb_topic top) {
		bd.insert(top);
		
	}

	
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	
	public void update(Tb_topic top) {
		StringBuffer sb=new StringBuffer("update Tb_topic set");
		List<Object>param=new ArrayList<Object>();
		if(top.getTopic_content()!=null&&!top.getTopic_content().equals("")){
			param.add(top.getTopic_content());
			sb.append(" topic_content=?");
		}
		if(top.getAnswer()!=null&&!top.getAnswer().equals("")){
			param.add(top.getAnswer());
			if(param.size()>0)
				sb.append(",answer=?");
			else
				sb.append(" answer=?");
		}
		if(top.getSubject_id()!=null&&!top.getSubject_id().equals("")){
			param.add(top.getSubject_id());
			if(param.size()>0)
				sb.append(",subject_id=?");
			else	
				sb.append(" subject_id=?");
		}
		if(top.getType_id()!=null&&!top.getType_id().equals("")){
			param.add(top.getType_id());
			if(param.size()>0)
				sb.append(",type_id=?");
			else
				sb.append(" type_id=?");
		}
		sb.append("where id=?");
		String sql=sb.toString();
		param.add(top.getTopic_id());
		bd.update(sql, param);
		
	}

	
	public List<Tb_topic> findall() {
		return bd.find("from Tb_topic", null);
	}

	public Tb_topic findbyid(int id) {
		
		return bd.findById(Tb_topic.class, id);
	}

	
	public List<Tb_topic> findbysub(int subtyp) {
		List<Object>param=new ArrayList<Object>();
		param.add(subtyp);
		
		return bd.find("from Tb_topic where subject_id=?", param);
	}

}
